package com.ctbri.io;

import java.io.File;
import java.io.IOException;

import com.ctbri.util.Directory;

public class Ex5_ProcessFiles {
	public interface Strategy {
		void process(File file);
	}
	
	private Strategy strategy;
	private String regex;
	
	public Ex5_ProcessFiles(Strategy stragegy, String regex) {
		this.strategy = stragegy;
		this.regex = regex;
	}
	
	public void start(String[] args) {
		try{
			if(args.length == 0) {
				processDirectoryTree(new File("."));
			} else {
				for(String arg : args) {
					File fileArg = new File(arg);
					if(fileArg.isDirectory())
						processDirectoryTree(fileArg);
					else {
						strategy.process(new File(arg).getCanonicalFile());
					}
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public void processDirectoryTree(File root) throws IOException {
		for(File file : Directory.walk(root.getAbsolutePath(), regex))
			strategy.process(file.getCanonicalFile());
	}
	
	public static void main(String[] args) {
		new Ex5_ProcessFiles(new Ex5_ProcessFiles.Strategy() {
			@Override
			public void process(File file) {
				System.out.println(file);
			}
		}, ".*").start(args);
	}
}
